package cn.jbolt.ec.order.handler;

import cn.jbolt.common.model.*;
import cn.jbolt.ec.rush.RushCommodityService;
import cn.jbolt.ec.retailer.RetailerService;
import cn.jbolt.ec.rush.AlreadyService;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;

/**
 * 全民秒杀
 */
public class TypeP {
    @Inject
    RetailerService retailerService;
    @Inject
    AlreadyService alreadyService;
    @Inject
    RushCommodityService rushCommodityService;



    public JSONObject handle(int client_id,int retailer_id,int commodity_id,
                             int option_id, String consignee, String mobile){

        /*
        生成新订单
         */
        final JSONObject jo = new JSONObject();
        Db.tx(() -> {
            BecRetailerOrder becRetailerOrder = new BecRetailerOrder();
            becRetailerOrder.setAddress("到店拿货")
                    .setMobile(mobile)
                    .setConsignee(consignee).setStatus(5)
                    .setType(15).setClientId(client_id).setRetailerId(retailer_id);

            long t =System.currentTimeMillis();

            String orderids = "j-"+t+'-'+client_id+'-'+retailer_id;
            becRetailerOrder.setOrderId(orderids).setOrderTime(t);
            if(!becRetailerOrder.save()) ; // 订单生成失败

            //  是否已经参加全民秒杀
            Kv  cond = Kv.by("type",15).set("member_id",client_id);
            int an = alreadyService.getCount(cond);
            // 已经参与
            if(an>0) {
                jo.put("ok",1);
                return false;
            }
            cond = Kv.by("retailer_id",retailer_id).set("commodity_id",commodity_id)
                    .set("option_id",option_id);
            BecRushstoreCommodity becRushstoreCommodity = rushCommodityService.findFirst(cond);
            BecMember m = new BecMember().dao().findByIdLoadColumns(client_id,"id,balance");
            // 商品不存在
            if(becRushstoreCommodity==null) {
                jo.put("ok",2);
                return false;
            }
            // 无库存
            if(becRushstoreCommodity.getRushStock()<1)  {
                jo.put("ok",3);
                return false;
            }
            // 余额不足
            Long balance = m.getBalance();
            if(m==null || balance==null || balance <becRushstoreCommodity.getRushPrice()) {
                jo.put("ok", 4);
                return false;
            }
            m.setBalance(m.getBalance()-becRushstoreCommodity.getRushPrice());

            if(!m.update()){
                jo.put("ok", 9);
                return false;

            }
            becRetailerOrder.setAmountPaid((long)becRushstoreCommodity.getRushPrice())
                    .setAmountTotal((long)becRushstoreCommodity.getRushPrice());
            // 平台余额已支付
            becRetailerOrder.setStatus(7);
            // 抢购失败
            becRushstoreCommodity.setRushStock(becRushstoreCommodity.getRushStock()-1);
            if(!becRushstoreCommodity.update()){
                jo.put("ok", 5);
                return false;

            }



            // 重用charge id放rushstorecommodityid

            becRetailerOrder.setChargeId(becRushstoreCommodity.getId());
            if(!becRetailerOrder.update()){
                jo.put("ok", 6);
                return false;

            }
            BecRushAlready becRushAlready = new BecRushAlready();
            boolean b = becRushAlready.setMemberId(client_id)
                    .setType(15).save();
            // 抢购失败
            if(!b){
                jo.put("ok", 7);
                return false;


            }

            // 抢购成功，请联系商家
            jo.put("ok", 0);
            jo.put("id",becRetailerOrder.getId());
            jo.put("order",becRetailerOrder);
            return true;

        });
        return jo;


    }

}
